package com.zjh.photoshopstudy.app.video;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.bumptech.glide.Glide;
import com.zjh.photoshopstudy.Model.VideoEntity;
import com.zjh.photoshopstudy.R;
import com.zjh.photoshopstudy.Utils.Data;


public class ShareDialog extends Activity {
    VideoEntity video;
    String url;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Window window = this.getWindow();
        window.getDecorView().setPadding(0, 0, 0, 0);
        WindowManager.LayoutParams layoutParams = window.getAttributes();
        layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
        layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
        layoutParams.gravity = Gravity.BOTTOM;
        window.setAttributes(layoutParams);
        setContentView(R.layout.activity_share_dialog);
        video = (VideoEntity) getIntent().getSerializableExtra("video");
        url = Data.webRoot_URL + video.getvThumbnailPath();
    }

    public void close(View view) {
        finish();
    }

    public void wx(View view) {
        initNetWorkImage(url, this, "wx");
    }

    public void pyq(View view) {
        initNetWorkImage(url, this, "pyq");

    }

    public void qq(View view) {
        initNetWorkImage(url, this, "qq");
    }

    public void wb(View view) {
        initNetWorkImage(url, this, "wb");
    }

    public void initNetWorkImage(final String imgUrl, final Activity context, final String to) {

        new AsyncTask<Void, Void, Bitmap>() {
            @Override
            protected Bitmap doInBackground(Void... params) {
                Bitmap bitmap = null;
                try {
                    bitmap = Glide.with(context)
                            .asBitmap()
                            .load(imgUrl)
                            .submit(360, 480).get();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return bitmap;
            }

            @Override
            protected void onPostExecute(Bitmap bitmap) {
                Uri uri = Uri.parse(MediaStore.Images.Media.insertImage(getContentResolver(), bitmap, null, null));
                Intent sendIntent = new Intent();
                sendIntent.setAction(Intent.ACTION_SEND);
                sendIntent.putExtra(Intent.EXTRA_STREAM, uri);
                sendIntent.setType("image/*");
                sendIntent.putExtra("Kdescription", video.getvTitle());
                sendIntent.putExtra(Intent.EXTRA_TEXT, video.getvContent()+"\n"+video.getvAppUserEntity().getuName() );   //附带的说明信息
                sendIntent.putExtra(Intent.EXTRA_SUBJECT, "PS Study");
                if (to.equals("pyq")) {
                    sendIntent.setClassName("com.tencent.mm", "com.tencent.mm.ui.tools.ShareToTimeLineUI");//微信朋友圈，仅支持分享图片
                } else if (to.equals("wx")) {
                    sendIntent.setClassName("com.tencent.mm", "com.tencent.mm.ui.tools.ShareImgUI");
                } else if (to.equals("qq")) {
                    sendIntent.setClassName("com.tencent.mobileqq", "com.tencent.mobileqq.activity.JumpActivity");
                } else if (to.equals("wb")) {
                    sendIntent.setPackage("com.sina.weibo");
                }

                startActivity(sendIntent);


            }
        }.execute();
    }


}
